$(function () {
    // 隐藏 表名列
    $("#table").hide();
    $("#packageDiv").hide();
    $("#search").hide();
    $("#daoPackageDiv").hide();
    // 是否指定表 true
    $("#specifiedTable1").click(function () {
        showTableNames(0,0);
    });
    // 是否指定表 false
    $("#specifiedTable2").click(function () {
        $("#table").hide();
        $("#search").hide();
        $("#tableMsg").show();
    });
    // 回车触发搜索表
    $("#tableName").keydown(function () {
        if (event.keyCode == "13"){
            showTableNames(0,0);
        }
    });
    // 右边弹出消息提示
    $(".form-control").click(function () {
        var msg = $(this).data("prompt");
        if (msg){
            layer.tips(msg, "#"+$(this).attr("id"),{tips: [2, '#78BA32']});
        }
    });
    $("#testConnection").click(function () {
        var param = $("#testConnectionForm").serialize();
        $.ajax({
            url: '/testConnection',
            contentType: "application/json; charset=utf-8",
            data: param,
            type: "get",
            cache: false,//false是不缓存，true为缓存
            async: true,//true为异步，false为同步
            success: function (result) {
                var msg = result.msg;
                var status = result.result;
                if(status === 200){
                    $("#testMsg").html(result.msg)
                }
                alert(msg);
            }
        })
    });
});

function packageFn(b) {
    if(b == true){
        $("#packageDiv").show();
    }else{
        $("#packageDiv").hide();
    }
}
// 分页
function showPage(totalPages,pageNum) {
    $('#page').html('<li><a href="JavaScript:void(0)" onclick="subOne()">&laquo;</a></li>');
    for (var star = 1;star<=totalPages;star ++){
        var page;
        if(pageNum == star){
            page = '<li class="disabled"><a href="JavaScript:void(0)" value="'+star+'" id="pageSele" >'+star+'</a></li>';
        }else {
            page = '<li><a href="JavaScript:void(0)" onclick="showTableNames(0,'+star+')">'+star+'</a></li>';
        }
        if(pageNum == 1 && star == 1){
            $('#page').html('<li class="disabled"><a href="JavaScript:void(0)">&laquo;</a></li>');
        }
        $('#page').html($('#page').html()+page);
        if(pageNum == totalPages && star == totalPages){
            $('#page').html($('#page').html()+'<li class="disabled"><a href="JavaScript:void(0)">&raquo;</a></li>')
        }else if(star == totalPages){
            $('#page').html($('#page').html()+'<li><a href="JavaScript:void(0)" onclick="addOne('+totalPages+')">&raquo;</a></li>')
        }
    }
}
// 显示数据库中的表名字
function showTableNames(pageSize,pageNum) {
    var tableName = $("#tableName").val();
    $("#tableMsg").hide();
    $("#table").show();
    $("#search").show();
    var tableNames = $("#tableNames");
    $.ajax({
        url: '/getTableNames?pageSize='+pageSize+'&pageNum='+pageNum + '&tableName='+tableName,
        contentType: "application/json; charset=utf-8",
        type: "get",
        cache: false,//false是不缓存，true为缓存
        async: true,//true为异步，false为同步
        success: function (result) {
            var msg = result.msg;
            var data = result.data;
            var status = result.result;
            console.info(msg,data,status);
            if(result.result === 200){
                showPage(result.data.totalPages,result.data.pageNum);
                tableNames.empty();
                $.each(result.data.data, function (index,name) {
                    var tableName = '<div class="checkbox"><label><input onclick="setTableNames()" name="tableNames" type="checkbox" value="';
                    tableName = tableName + name + '">' + name +'</label></div>';
                    tableNames.html(tableNames.html() + tableName);
                });
            }else {
                alert(result.msg);
            }
        }
    });
}
function addOne(totalPage) {
    var val = $("#pageSele").attr("value")*1 + 1;
    showTableNames(10,val)
}
function subOne() {
    var val = $("#pageSele").attr("value")*1;
    showTableNames(10,val-1)
}
function checkOut(check) {
    $("#"+check+" input[type='checkbox']").each(function (i,v) {
        v.click()
    })
}
function daoPackagePathCreate(bool) {
    console.info(bool);
    if (bool) {
        $("#daoPackageDiv").show()
    } else {
        $("#daoPackageDiv").hide()
    }
}
function setTableNames() {
    var tableNames = new Array();
    $("#tableNames input:checked").each(function (i, v) {
        tableNames[i]= $(v).val();
    });
    $("#tableNamesBak").val(tableNames)
}